<!--
 * @Description:
 * @Version: 1.0
 * @Autor: haozhuang
 * @Date: 2020-04-28 14:34:17
 * @LastEditors: haozhuang
 * @LastEditTime: 2020-05-08 15:02:15
 -->
<template>
  <div id="home" class="el-row--flex">
    <div>
      <h3 class="title">
        项目目录 - <span>{{ projectList.total }} 项目</span>
      </h3>
      <el-scrollbar>
        <div class="list el-row--flex">
          <dl class="project add" @click="addProject">
            <dt><img src="@/assets/home/add.png" alt=""></dt>
            <dd>
              项目名称
              <!-- <img src="@/assets/home/delete.png" alt="">
            <img src="@/assets/home/edit.png" alt=""> -->
            </dd>
          </dl>
          <dl
            v-for="(item, index) in projectList.dataList"
            :key="index"
            class="project"
            :class="{ active: Number(projectId) === item.id }">
            <dt @click="selectProject(item.id, item.name)">
              <el-image
                fit="contain"
                :src="getLogo('project', item.id)"
                lazy
                scroll-container=".list"
              >
                <div slot="error" class="image-error-slot">
                  <i class="el-icon-picture-outline" />
                </div>
              </el-image>
            </dt>
            <dd>
              {{ item.name }}

              <Popconfirm
                v-if="userInfo.userId === item.userId"
                :confirm-callback="deleteProject"
                :data="item"
              >
                <template>
                  <img src="@/assets/home/delete2.png" alt="">
                </template>
              </Popconfirm>

              <!-- <el-popover
              v-model="visible"
              placement="bottom"
              popper-class="Popconfirm"
              trigger="click"
            >
              <p>删除</p>
              <p class="el-row--flex is-justify-center is-align-middle">是否删除该项目</p>
              <div class="el-row--flex is-justify-space-around">
                <el-button>取消</el-button>
                <el-button type="primary">确定</el-button>
              </div>
              <img slot="reference" src="@/assets/home/delete2.png" alt="">
            </el-popover> -->

              <img
                v-if="userInfo.userId === item.userId"
                src="@/assets/home/edit2.png"
                alt=""
                @click.stop="editProject(item)"
              >
            </dd>
          </dl>
        </div>
      </el-scrollbar>

      <addProjectDialog
        :add-project-data="addProjectData"
        @colseDialog="colseDialog"
      />
    </div>
  </div>
</template>

<script>
// import { mapGetters } from 'vuex'

import { getProjectPage, deleteProject } from '@/api/home'
import addProjectDialog from './addProject'
export default {
  name: 'Home',
  components: { addProjectDialog },
  data() {
    return {
      projectList: [],
      userInfo: JSON.parse(sessionStorage.userInfo),
      projectId: sessionStorage.projectId ? sessionStorage.projectId : '',
      searchName: '',
      addProjectData: {
        title: '添加项目',
        dialogFormVisible: false,
        iconPath: '',
        name: '',
        ename: '',
        description: ''
      }
    }
  },
  computed: {},
  created() {
    console.log(this.$store)
    this.getProjectList()
    this.$store.dispatch('index/getShareToMyStatus')
  },
  methods: {
    selectProject(id, name) {
      this.projectId = id
      sessionStorage.projectId = id
      sessionStorage.projectName = name
      this.$store.commit('index/set_projectName', name)
      this.$router.push({ name: 'project' })
      document.querySelector('.menu').children[1].style.display = 'block'
      document.querySelector('.menu').children[2].style.display = 'block'
      document.querySelector('.menu').children[3].style.display = 'block'
    },
    getProjectList() {
      getProjectPage(200, 1, this.searchName)
        .then(result => {
          console.log(result)
          const { data } = result
          console.log(data)
          this.projectList = data
          // sessionStorage.projectId = data.dataList[0].id
          // this.projectId = data.dataList[0].id
        })
        .catch(err => {
          console.log(err)
        })
    },
    addProject() {
      this.addProjectData = {
        id: '',
        dialogFormVisible: true,
        title: '添加项目'
      }
    },
    editProject(item) {
      console.log(item)
      this.addProjectData = {
        ...this.addProjectData,
        ...item,
        ...{
          dialogFormVisible: true,
          title: '编辑项目'
        }
      }

      this.$forceUpdate()
    },
    deleteProject(item) {
      console.log(item.id)
      deleteProject(item.id)
        .then(result => {
          this.projectList.dataList.splice(
            this.projectList.dataList.findIndex(v => v.id === item.id),
            1
          )
          console.log(result)
        })
        .catch(err => {
          console.log(err)
        })
    },
    colseDialog() {
      this.addProjectData.dialogFormVisible = false
      setTimeout(() => {
        this.getProjectList()
      }, 300)
    }
  }
}
</script>

<style lang="scss" scoped >
#home {
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 1);
  // border: 16px solid #fff;
    padding: 16px;
  flex-direction: column;

  & > div {
    width: 100%;
    height: 100%;
    border-radius: 15px;
    background: rgba(249, 249, 249, 1);
    padding-left: 59px;
  }

  .title {
    height: 86px;
    font-size: 20px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: rgba(44, 161, 213, 1);
    line-height: 86px;
    margin: 0;
    // &:after {
    //   content: "";
    //   display: block;
    //   width: 90px;
    //   height: 2px;
    //   background: rgba(44, 161, 213, 1);
    // }

    span {
      height: 24px;
      font-size: 14px;
      font-family: DINAlternate-Bold, DINAlternate;
      font-weight: normal;
      color: rgba(44, 161, 213, 0.5);
      line-height: 21px;
    }
  }

  .list {
    flex: 1;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .project {
    width: 250px;
    display: inline-block;
    background: rgba(255, 255, 255, 1);
    box-shadow: 2px 2px 14px 0px rgba(0, 0, 0, 0.1);
    border-radius: 13px;
    padding: 11px 11px 0 12px;
    margin-right: 40px;

    &.active {
      border: 1px solid #3ec5ef;
    }

    &.add {
      dd {
        color: rgba(190, 190, 190, 1);
      }
      dt {
        padding-top: 40px;

        img {
          width: 58px;
          height: auto;
          display: block;
          margin: 0 auto;
        }
      }
    }

    dt {
      width: 100%;
      height: 134px;
      background: rgba(190, 190, 190, 0.2);
      border-radius: 7px;

      img {
        width: 100%;
        height: 100%;
        border-radius: 7px;
      }
    }

    dd {
      padding: 9px 0;
      margin: 0;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #717171;

      img {
        width: 14px;
        float: right;
        margin-left: 10px;
      }
    }
  }
}
</style>
<style lang="scss">
#home {
  .image-error-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: #f5f7fa;
    color: #909399;
    font-size: 30px;
  }
  .project {
    dt {
      img {
        width: 100%;
        height: 100%;
        border-radius: 8px;
      }
    }
  }
}
</style>
